package com.supms.course.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.supms.course.mapper.TestReviewMistakeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.TestReviewMapper;
import com.supms.course.domain.TestReview;
import com.supms.course.service.ITestReviewService;

/**
 * 本科试卷复查Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-03-08
 */
@Service
public class TestReviewServiceImpl implements ITestReviewService 
{
    @Autowired
    private TestReviewMapper testReviewMapper;

    @Autowired
    private TestReviewMistakeMapper mistakeMapper;

    /**
     * 查询本科试卷复查
     * 
     * @param id 本科试卷复查主键
     * @return 本科试卷复查
     */
    @Override
    public TestReview selectTestReviewById(Long id)
    {
        return testReviewMapper.selectTestReviewById(id);
    }

    /**
     * 查询本科试卷复查列表
     * 
     * @param testReview 本科试卷复查
     * @return 本科试卷复查
     */
    @Override
    public List<TestReview> selectTestReviewList(TestReview testReview)
    {
        String operName = SecurityUtils.getUsername();
        testReview.setCreateBy(operName);
        return testReviewMapper.selectTestReviewList(testReview);
    }

    /**
     * 新增本科试卷复查
     * 
     * @param testReview 本科试卷复查
     * @return 结果
     */
    @Override
    public int insertTestReview(TestReview testReview)
    {
        String operName = SecurityUtils.getUsername();
        testReview.setCreateBy(operName);
        testReview.setSupName(testReviewMapper.getSupNameByOperName(operName));
        testReview.setCreateTime(DateUtils.getNowDate());
        return testReviewMapper.insertTestReview(testReview);
    }

    /**
     * 修改本科试卷复查
     * 
     * @param testReview 本科试卷复查
     * @return 结果
     */
    @Override
    public int updateTestReview(TestReview testReview)
    {
        String operName = SecurityUtils.getUsername();
        testReview.setUpdateBy(operName);
        testReview.setUpdateTime(DateUtils.getNowDate());
        return testReviewMapper.updateTestReview(testReview);
    }

    /**
     * 批量删除本科试卷复查
     * 
     * @param ids 需要删除的本科试卷复查主键
     * @return 结果
     */
    @Override
    public int deleteTestReviewByIds(Long[] ids)
    {
        // 先查询是否有错误成绩登记，有则先做提醒
        Long[] mistakeIds = mistakeMapper.selectMistakeByTRIds(ids);
        if (mistakeIds.length > 0) {
            return 5000;
        } else {
            // 判断试卷复查表中是否做了评价，有则先删除评价表
            Long[] haveIds = testReviewMapper.selectReviewEvalFlagByIds(ids);
            if (haveIds.length > 0) {
                testReviewMapper.deleteTestReviewEvalByIds(haveIds);
            }
            return testReviewMapper.deleteTestReviewByIds(ids);
        }
    }

    /**
     * 删除本科试卷复查信息
     * 
     * @param id 本科试卷复查主键
     * @return 结果
     */
    @Override
    public int deleteTestReviewById(Long id)
    {
        return testReviewMapper.deleteTestReviewById(id);
    }

    /**
     * 获取本科试卷复查表（数据综合查看表）
     * @param testReview 接收到要查看表的对象
     * @return 表内容及评价结果对象
     */
    @Override
    public TestReview getTestReviewTable(TestReview testReview) {
        // 判断是否已经评价，没有评价的话不查询评价结果
        String reviewEvalFlag = testReview.getReviewEvalFlag();
        // "0" 为未评价，"1" 为已评价
        if ("0".equals(reviewEvalFlag)) {
            return testReviewMapper.getTestReviewTable0(testReview);
        } else if ("1".equals(reviewEvalFlag)) {
            return testReviewMapper.getTestReviewTable1(testReview);
        } else return null;
    }
}
